//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "VolatilityCube.h"
using namespace Cephei::QL::Experimental::Volatility;
#include <gen/QL/Experimental/Volatility/AbcdAtmVolCurve.h>
#include <gen/QL/Experimental/Volatility/InterestRateVolSurface.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Volatility::CVolatilityCube::CVolatilityCube (Cephei::IVector<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^ surfaces, Cephei::IVector<Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^>^ curves) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CoVector<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^ _Csurfaces;
    CoVector<Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^>^ _Ccurves;
    try
    {
#ifdef HANDLE
        _phVolatilityCube = NULL;
#endif
        _Csurfaces = safe_cast<CoVector<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^> (surfaces);
        _Csurfaces ->Lock ();
        INativeVector<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^ _NCIsurfaces = _Csurfaces->getFeature (NativeFeature::Handle);
        CInterestRateVolSurfaceVector^ _NCsurfaces = safe_cast<CInterestRateVolSurfaceVector^>(_NCIsurfaces);
        std::vector<Handle<QuantLib::InterestRateVolSurface> >& _surfaces = static_cast<std::vector<Handle<QuantLib::InterestRateVolSurface> >&> (_NCsurfaces->GetHandle ());
        _Ccurves = safe_cast<CoVector<Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^>^> (curves);
        _Ccurves ->Lock ();
        INativeVector<Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^>^ _NCIcurves = _Ccurves->getFeature (NativeFeature::Handle);
        CAbcdAtmVolCurveVector^ _NCcurves = safe_cast<CAbcdAtmVolCurveVector^>(_NCIcurves);
        std::vector<Handle<QuantLib::AbcdAtmVolCurve> >& _curves = static_cast<std::vector<Handle<QuantLib::AbcdAtmVolCurve> >&> (_NCcurves->GetHandle ());
        _ppVolatilityCube = new boost::shared_ptr<QuantLib::VolatilityCube> (new QuantLib::VolatilityCube ( _surfaces,  _curves ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Csurfaces != nullptr) _Csurfaces->Unlock();
        if (_Ccurves != nullptr) _Ccurves->Unlock();
    }
}
Cephei::QL::Experimental::Volatility::CVolatilityCube::CVolatilityCube (boost::shared_ptr<QuantLib::VolatilityCube>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phVolatilityCube = NULL;
#endif
	_ppVolatilityCube = &childNative;
    
}
Cephei::QL::Experimental::Volatility::CVolatilityCube::CVolatilityCube (QuantLib::VolatilityCube& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phVolatilityCube = NULL;
#endif
	_ppVolatilityCube = new boost::shared_ptr<QuantLib::VolatilityCube> (&childNative);
    
    _VolatilityCubeOwner = owner;
    
}

Cephei::QL::Experimental::Volatility::CVolatilityCube::CVolatilityCube (CVolatilityCube^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phVolatilityCube = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppVolatilityCube = new boost::shared_ptr<QuantLib::VolatilityCube> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Volatility::CVolatilityCube::CVolatilityCube (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phVolatilityCube = NULL;
#endif
	if (!t->IsSubclassOf(CVolatilityCube::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Volatility::CVolatilityCube::CVolatilityCube (QuantLib::Handle<QuantLib::VolatilityCube>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phVolatilityCube = &childNative;
	_ppVolatilityCube = &static_cast<boost::shared_ptr<QuantLib::VolatilityCube>>(childNative.currentLink());
    
    _VolatilityCubeOwner = owner;
}
Cephei::QL::Experimental::Volatility::CVolatilityCube::CVolatilityCube (QuantLib::Handle<QuantLib::VolatilityCube> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phVolatilityCube = &childNative;
	_ppVolatilityCube = &static_cast<boost::shared_ptr<QuantLib::VolatilityCube>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Volatility::CVolatilityCube::CVolatilityCube (QuantLib::VolatilityCube childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phVolatilityCube = NULL;
#endif
	_ppVolatilityCube = new boost::shared_ptr<QuantLib::VolatilityCube> (new QuantLib::VolatilityCube (childNative));
    
}
#endif

Cephei::QL::Experimental::Volatility::CVolatilityCube::~CVolatilityCube ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppVolatilityCube != NULL)
    {
	    delete _ppVolatilityCube;
        _ppVolatilityCube = NULL;
    }
}
Cephei::QL::Experimental::Volatility::CVolatilityCube::!CVolatilityCube ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppVolatilityCube != NULL)
    {
	    delete _ppVolatilityCube;
    }
}
QuantLib::VolatilityCube& Cephei::QL::Experimental::Volatility::CVolatilityCube::GetReference ()
{
    if (_ppVolatilityCube == NULL) throw gcnew NativeNullException ();
	return **_ppVolatilityCube;
}
boost::shared_ptr<QuantLib::VolatilityCube>& Cephei::QL::Experimental::Volatility::CVolatilityCube::GetShared ()
{
    if (_ppVolatilityCube == NULL) throw gcnew NativeNullException ();
	return *_ppVolatilityCube;
}
QuantLib::VolatilityCube* Cephei::QL::Experimental::Volatility::CVolatilityCube::GetPointer ()
{
    if (_ppVolatilityCube == NULL) throw gcnew NativeNullException ();
	return &**_ppVolatilityCube;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::VolatilityCube>& Cephei::QL::Experimental::Volatility::CVolatilityCube::GetHandle ()
{
	if (_phVolatilityCube == NULL)
	{
		_phVolatilityCube = new Handle<QuantLib::VolatilityCube> (*_ppVolatilityCube);
	}
	return *_phVolatilityCube;
}
#endif
bool Cephei::QL::Experimental::Volatility::CVolatilityCube::HasNative () 
{
	return (_ppVolatilityCube != NULL);
}

Cephei::IVector<Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^>^ Cephei::QL::Experimental::Volatility::CVolatilityCube::Curves::get ()
{
    try
    {
    	std::vector<Handle<QuantLib::AbcdAtmVolCurve> >& _rv = (std::vector<Handle<QuantLib::AbcdAtmVolCurve> >&)(*_ppVolatilityCube)->curves ( );   
        CoVector<Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^>^ _nrv = gcnew CoVector<Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^>(gcnew CAbcdAtmVolCurveVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^ Cephei::QL::Experimental::Volatility::CVolatilityCube::Surfaces::get ()
{
    try
    {
    	std::vector<Handle<QuantLib::InterestRateVolSurface> >& _rv = (std::vector<Handle<QuantLib::InterestRateVolSurface> >&)(*_ppVolatilityCube)->surfaces ( );   
        CoVector<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^ _nrv = gcnew CoVector<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>(gcnew CInterestRateVolSurfaceVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Volatility::IVolatilityCube^ Cephei::QL::Experimental::Volatility::CVolatilityCube_Factory::Create (Cephei::IVector<Cephei::QL::Experimental::Volatility::IInterestRateVolSurface^>^ surfaces, Cephei::IVector<Cephei::QL::Experimental::Volatility::IAbcdAtmVolCurve^>^ curves)
{
    return gcnew CVolatilityCube ( surfaces,  curves);
}
